package com.aduerr.v1shouyin.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Looper;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Utils {

    /**
     * 判断当前是否有可用的网络环境
     *
     * @return
     */
    public static boolean isNetworkAvailable(Context context) {
        // 获取手机所有连接管理对象（包括对wi-fi,net等连接的管理）
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

        if(networkInfo != null && networkInfo.length > 0) {
            for(int i = 0; i < networkInfo.length; i++) {
                if(networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
        return false;
    }

    public static String getMoneyDesc(String pre, String payMoney) {
        return pre + "<font color='#e3691a'>￥" + payMoney + "</font>元";
    }


    public static String getVersionName(Context context) {
        PackageManager packageManager = context.getPackageManager();
        try {
            PackageInfo packInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packInfo.versionName;
        } catch(NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    public static float dpToPx(Context context, float value) {
        return context.getResources().getDisplayMetrics().density * value;
    }

    public static boolean isMainThread() {
        return Looper.getMainLooper() == Looper.myLooper();
    }

    /**
     * 将16进制的String转化为字节数组
     *
     * @param hexString
     * @return
     */
    public static byte[] hexString2bytes(String hexString) {
        hexString = hexString.toUpperCase();
        byte[] b = new byte[hexString.length() / 2];
        char[] hexStringByte = hexString.toCharArray();
        for(int i = 0; i < hexString.length() / 2; i++) {
            byte byteTemp =
                    (byte) ((toByte(hexStringByte[i * 2]) << 4) | toByte(hexStringByte[i * 2 + 1]));
            b[i] = byteTemp;
        }
        return b;
    }

    public static byte toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }

    /**
     * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss  *   * @param dateDate  * @return
     */
    public static String dateToStrLong(long date) {
        Date dateDate = new Date(date);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    public static String getAmountString(long amount) {
        java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        String str = "" + nf.format((double) amount / (double) 100);
        int index = str.lastIndexOf(".");
        if(index == -1) {
            str += ".00";
            return str;
        }
        if(str.lastIndexOf(".") == str.length() - 2)
            str += "0";
        return str;
    }

    public static Bitmap invertBitmap(Bitmap bitmap) {
//		int sWidth = bitmap.getWidth();
//		int sHeight = bitmap.getHeight();
//		int[] sPixels = new int[sWidth * sHeight];
//		bitmap.getPixels(sPixels, 0, sWidth, 0, 0, sWidth, sHeight);
//
//		int sIndex = 0;
//		for (int sRow = 0; sRow < sHeight; sRow++) {
//			sIndex = sRow * sWidth;
//			for (int sCol = 0; sCol < sWidth; sCol++) {
//				int sPixel = sPixels[sIndex];
//				int sA = (sPixel >> 24) & 0xff;
//				int sR = (sPixel >> 16) & 0xff;
//				int sG = (sPixel >> 8) & 0xff;
//				int sB = sPixel & 0xff;
//
//				sR = 255 - sR;
//				sG = 255 - sG;
//				sB = 255 - sB;
//
//				sPixel = ((sA & 0xff) << 24 | (sR & 0xff) << 16 | (sG & 0xff) << 8 | sB
//				& 0xff);
//
//				sPixels[sIndex] = sPixel;
//
//				sIndex++;
//			}
//		}
//		bitmap.setPixels(sPixels, 0, sWidth, 0, 0, sWidth, sHeight);
//		return bitmap;

        int[] pix = new int[bitmap.getWidth() * bitmap.getHeight()];
        boolean isBitmapEmpty = true;
        for(int y = 0; y < bitmap.getHeight(); y++) {
            for(int x = 0; x < bitmap.getWidth(); x++) {
                int index = y * bitmap.getWidth() + x;
                pix[index] = Color.WHITE;
                if(bitmap.getPixel(x, y) != Color.TRANSPARENT) {
                    isBitmapEmpty = false;
                    pix[index] = Color.BLACK;
                }
            }
        }
        if(isBitmapEmpty) {
            return null;
        }
        bitmap.setPixels(pix, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
        return bitmap;

    }

}
